//
//  HNSH_ConfirmOrderViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/26.
//

#import "HNSH_ConfirmOrderViewController.h"

//view
#import "HNSH_ConfirmOrderAddressCell.h"
#import "HNSH_ConfirmOrderGoodsCell.h"
#import "HNSH_ConfirmOrderGoodsHeader.h"
#import "HNSH_ConfirmOrderGoodsFooter.h"

#import "HNSH_ConfirmOrderBottomView.h"

//vc
#import "HNSH_AddressViewController.h"          //地址
#import "HNSH_CashierPayViewController.h"       //收银台

@interface HNSH_ConfirmOrderViewController ()

/** */
@property (nonatomic, strong)HNSH_AddressModel * addressModel;

/* bottomView */
@property (nonatomic, strong) HNSH_ConfirmOrderBottomView *bottomView;

/* listAry */
@property (nonatomic, strong) NSArray *listAry;

/* userNoteAry */
@property (nonatomic, strong) NSMutableArray *userNoteAry;
@end

static NSString *HNSH_ConfirmOrderAddressCellID = @"HNSH_ConfirmOrderAddressCellID";
static NSString *HNSH_ConfirmOrderGoodsCellID = @"HNSH_ConfirmOrderGoodsCellID";
static NSString *HNSH_ConfirmOrderGoodsHeaderID = @"HNSH_ConfirmOrderGoodsHeaderID";
static NSString *HNSH_ConfirmOrderGoodsFooterID = @"HNSH_ConfirmOrderGoodsFooterID";

@implementation HNSH_ConfirmOrderViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self showNavTitle:@"确认订单" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor appBGColor];
    [self loadNewData];
    
    [self configUI];
    
    [self configTableView];
}

//MARK: 获取订单信息
- (void)loadNewData{
    [HNSH_ShopNetServer requestGoodsOrderConfirmWithGoodsId:self.goods_id spec_id:self.spec_id quantity:self.quantity from_cart:self.from_cart address_id:self.addressModel.uid success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            //默认地址
            NSDictionary * address = responseObject.resultset[@"address"];
            HNSH_AddressModel * addressModel = [HNSH_AddressModel yy_modelWithDictionary:address];
            if (![addressModel.uid isEqualToString:@"0"]) {
                //默认地址
                self.addressModel = addressModel;
                [self refreshHeader];
            }
            self.bottomView.moneyString = [NSString stringWithFormat:@"%@",responseObject.resultset[@"summary_reward"]];
            //订单信息
            self.listAry = [NSArray yy_modelArrayWithClass:[HNSH_ConfirmOrderModel class] json:responseObject.resultset[@"items"]];
            
            //创建默认留言
            for (HNSH_ConfirmOrderModel *model in self.listAry) {
                NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"shop_id":model.shop_id,@"user_note":@""}];
                [self.userNoteAry addSafeObject:dict];
            }
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)chooseAddress{
    HNSH_AddressViewController * vc = [HNSH_AddressViewController new];
    vc.isChoose = YES;
    WeakSelf(weakSelf);
    vc.chooseAddressBlock = ^(HNSH_AddressModel * _Nonnull addressModel) {
        weakSelf.addressModel = addressModel;
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        HNSH_ConfirmOrderAddressCell *cell = [weakSelf.tableView cellForRowAtIndexPath:indexPath];
        cell.model = addressModel;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)refreshHeader{
//    if (self.addressModel) {
//        self.headerView = nil;
//        self.tableView.tableHeaderView = self.headerView;
//    }
//    self.headerView.addressModel = self.addressModel;
}

//MARK: 提交订单
- (void)submit{
    if (![self.addressModel.uid isNotBlank]) {
        [self.view makeToast:@"请选择收货地址"];
        return;
    }
    [HNSH_ShopNetServer requestGoodsOrderSubmitWithGoodsId:self.goods_id spec_id:self.spec_id quantity:self.quantity from_cart:self.from_cart address_id:self.addressModel.uid user_note:@[] success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSDictionary * data = responseObject.resultset;
            HNSH_CashierPayModel * model = [HNSH_CashierPayModel yy_modelWithDictionary:data];
            [self pushVCWithModel:model];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)pushVCWithModel:(HNSH_CashierPayModel *)model{
    HNSH_CashierPayViewController * vc = [HNSH_CashierPayViewController new];
    vc.payModel = model;
    [self.navigationController pushViewController:vc animated:YES];
}

//MARK: Getter Methods
- (HNSH_ConfirmOrderBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[HNSH_ConfirmOrderBottomView alloc]init];
        WeakSelf(weakSelf);
        _bottomView.clickBlock = ^{
            [weakSelf submit];
        };
    }
    return _bottomView;
}

- (NSMutableArray <NSMutableDictionary *>*)userNoteAry {
    if (!_userNoteAry) {
        _userNoteAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _userNoteAry;
}

- (void)configUI {
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kBottomSafeHeight+62);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(kNavBarAndStatusBarHeight, 0, kBottomSafeHeight+62, 0));
    }];
}

- (void)configTableView {
    [self.tableView registerClass:[HNSH_ConfirmOrderAddressCell class] forCellReuseIdentifier:HNSH_ConfirmOrderAddressCellID];
    [self.tableView registerClass:[HNSH_ConfirmOrderGoodsCell class] forCellReuseIdentifier:HNSH_ConfirmOrderGoodsCellID];
    [self.tableView registerClass:[HNSH_ConfirmOrderGoodsHeader class] forHeaderFooterViewReuseIdentifier:HNSH_ConfirmOrderGoodsHeaderID];
    [self.tableView registerClass:[HNSH_ConfirmOrderGoodsFooter class] forHeaderFooterViewReuseIdentifier:HNSH_ConfirmOrderGoodsFooterID];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1+self.listAry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    HNSH_ConfirmOrderModel *model = self.listAry[section-1];
    return model.items.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01f;
    }
    return 48.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01f;
    }
    return 186.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section != 0) {
        HNSH_ConfirmOrderModel *model = self.listAry[section-1];
        HNSH_ConfirmOrderGoodsFooter *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HNSH_ConfirmOrderGoodsFooterID];
        footer.model =  model;
        WeakSelf(weakSelf);
        footer.footerBlock = ^(NSString * _Nonnull userNote) {
            NSMutableDictionary *dict = weakSelf.userNoteAry[section-1];
            [dict setValue:userNote forKey:@"user_note"];
        };
        return footer;
    }
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section != 0) {
        HNSH_ConfirmOrderModel *model = self.listAry[section-1];
        HNSH_ConfirmOrderGoodsHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HNSH_ConfirmOrderGoodsHeaderID];
        if (model && [model.shop_name isNotBlank]) {
            header.titleString = model.shop_name;
        }
        return header;
    }
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 88;
    }
    return 106;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HNSH_ConfirmOrderAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_ConfirmOrderAddressCellID];
        cell.model = self.addressModel;
        return cell;
    } else {
        HNSH_ConfirmOrderModel *model = self.listAry[indexPath.section-1];
        HNSH_ConfirmOrderGoodsModel *item = model.items[indexPath.row];
        HNSH_ConfirmOrderGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_ConfirmOrderGoodsCellID];
        cell.model = item;
        return cell;
    }
    return [UITableViewCell new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        [self chooseAddress];
    }
}

@end
